package view;

import lib.Bill;
import lib.TwoColumnField;
import lib.TwoColumnFieldManager;
import model.DB;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FocusChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.NullField;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.decor.BorderFactory;

public class BillCrumb extends VerticalFieldManager implements FocusChangeListener
{
	public static final int COLOR_ONE = Color.WHITESMOKE;
	public static final int COLOR_TWO = Color.WHEAT;

	public static final Background FOCUSBACKGROUND = BackgroundFactory.createSolidTransparentBackground(Color.AQUAMARINE, 40);
	
	private final int TOP_PADDING = 10;
	private final int BOTTOM_PADDING = 10;
	private final int LEFT_PADDING = 5;
	private final int RIGHT_PADDING = 5;
	private final int INNER_PADDING_TOP_BOTTOM = 8;
	
	private Bill bill;
	private NullField dummy;
	
	private Background background;
	public BillCrumb(Bill bill, Background background)
	{
		super(USE_ALL_WIDTH);
		doLayout();
		
		this.setBill(bill);
		
		LabelField title = new LabelField(bill.title());
		title.setPadding(new XYEdges(TOP_PADDING, RIGHT_PADDING, INNER_PADDING_TOP_BOTTOM, LEFT_PADDING));
		
		add(title);
		
		LabelField amountDue = new LabelField(DB.CURRENCY +" " + bill.getAmountOwed());
		LabelField dateDue = new LabelField();
		
		
		TwoColumnField twoCol = new TwoColumnField(amountDue, dateDue);
		TwoColumnFieldManager fManager = new TwoColumnFieldManager(Graphics.getScreenWidth() / 2,USE_ALL_WIDTH);
		fManager.add(twoCol);
		fManager.setPadding(new XYEdges(INNER_PADDING_TOP_BOTTOM, RIGHT_PADDING, BOTTOM_PADDING, LEFT_PADDING));
		
		add(fManager);
		
		dummy = new NullField();
		dummy.setFocusListener(this);
		this.add(dummy);
		
		this.setHoldBackground(background);
	}

	private void doLayout() 
	{
		this.setBorder(BorderFactory.createSimpleBorder(new XYEdges(1,0,1,0)));		
	}

	public void setBill(Bill bill) {
		this.bill = bill;
	}

	public Bill getBill() {
		return bill;
	}

	public void setHoldBackground(Background background)
	{
		if(background != null)
		{
			this.background = background;
			this.setBackground(this.background);
		}else
		{
			this.background = BackgroundFactory.createSolidBackground(Color.WHITESMOKE);
		}
		
	}
	
	public void focusChanged(Field field, int eventType) 
	{
		if(field == dummy)
		{
			if(dummy.isFocus())
			{
				this.setBackground(FOCUSBACKGROUND);
				// Set background Color to highlight if has focus
			}else
			{
				this.setBackground(this.background);
			}		
		}
	}
}
